package com.nciae.controller;


import com.nciae.exception.SellException;
import com.nciae.form.CategoryForm;
import com.nciae.po.ProductCategory;
import com.nciae.service.IProductCategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author YangChen
 * @since 2021-10-14
 */
@Controller
@RequestMapping("/productCategory")
public class ProductCategoryController {

    @Autowired
    private IProductCategoryService productCategoryService;

    @GetMapping("/list")
    public ModelAndView list(Map<String, Object> map) {
        List<ProductCategory> productCategoryList = productCategoryService.findAll();
        map.put("categoryList",productCategoryList);
        return new ModelAndView("category/list" ,map);
    }


    @GetMapping("/index")
    public ModelAndView index(@RequestParam(value = "categoryId", required = false) Integer categoryId, Map<String, Object> map) {

        if (categoryId != null) {
            ProductCategory one = productCategoryService.findOne(categoryId);
            map.put("productCategory", one);
            return new ModelAndView("category/index" ,map);
        }
        return new ModelAndView("category/insert" ,map);
    }




    @PostMapping("/update")
    public ModelAndView update(@Valid CategoryForm categoryForm,
                             BindingResult bindingResult,
                             Map<String, Object> map) {
        if (bindingResult.hasErrors()) {
            map.put("msg", bindingResult.getFieldError().getDefaultMessage());
            map.put("url", "/productCategory/index");
            return new ModelAndView("common/error" ,map);
        }
        ProductCategory productCategory = new ProductCategory();
        try {
            if (categoryForm.getCategoryId()!=null) {
                productCategory = productCategoryService.findOne(categoryForm.getCategoryId());
            }
            BeanUtils.copyProperties(categoryForm, productCategory);
            productCategoryService.update(productCategory);
        }catch (Exception e) {
            map.put("msg", bindingResult.getFieldError().getDefaultMessage());
            map.put("url", "/productCategory/index");
            return new ModelAndView("common/error" ,map);
        }

        map.put("url","/productCategory/list");
        return new ModelAndView("common/success" ,map);
    }

    @PostMapping("/insert")
    public ModelAndView insert(@Valid CategoryForm categoryForm,
                               BindingResult bindingResult,
                               Map<String, Object> map) {
        if (bindingResult.hasErrors()) {
            map.put("msg", bindingResult.getFieldError().getDefaultMessage());
            map.put("url", "/productCategory/insert");
            return new ModelAndView("common/error" ,map);
        }
        try {
            ProductCategory productCategory = new ProductCategory();
            BeanUtils.copyProperties(categoryForm, productCategory);
            productCategoryService.insert(productCategory);
        }catch (Exception e) {
            map.put("msg", bindingResult.getFieldError().getDefaultMessage());
            map.put("url", "/productCategory/insert");
            return new ModelAndView("common/error" ,map);
        }

        map.put("url","/productCategory/list");
        return new ModelAndView("common/success" ,map);
    }



    @GetMapping("/delete")
    public ModelAndView delete(@RequestParam(value = "categoryId", required = false) Integer categoryId, Map<String, Object> map) {
       productCategoryService.delete(categoryId);
        map.put("url","/productCategory/list");
        return new ModelAndView("common/success" ,map);
    }


}
